<?php 
defined('IN_YYS')or exit('Access Denied.');
System::DOWN_App_class('base','member','no');
System::DOWN_App_fun('user','index');
class cartzg extends base {
	private $gouwuchelist;
	
	public function __construct() {			
		$this->Cartlistzg = _getcookie('Cartlistzg');
		$this->db = System::DOWN_sys_class("model");
		
	}
	 
	
	//购物车商品列表
	public function cartlistzg(){
 		$key="购物车";
		$biaoti="购物车";
		$index="i3";
        $webname=$this->_yys['web_name'];	
		$Mcartlist=json_decode(stripslashes($this->Cartlistzg),true);
		
		$shopids='';	
		if(is_array($Mcartlist)){			
			foreach($Mcartlist as $key => $val){
				if($key != 'MoenyCount'){
					$shopids .= $key.',';
				}
				//$shopids .= intval($key).',';					
			}
			$shopids=str_replace(',0','',$shopids);
			$shopids=trim($shopids,',');
		}
		

		$yyslist=array();
		if($shopids!=NULL){
			//改造YYSLIST
			$shopidsArray = explode(',',$shopids);
			foreach($shopidsArray as $key => $one){
				if(is_numeric($one)){
					$shopinfo = $this->db->YOne("select * from `@#_zg_shangpin` where `id` = '$one'");
				}elseif($one != 'MoenyCount'){
					$ids = explode('_',$one);
					$ids = $ids['0'];
					$shopinfo = $this->db->YOne("select * from `@#_zg_shangpin` where `id` = '$ids'");
				}
				$yyslist[$one] = $shopinfo;	
			}
			//$yyslist=$this->db->Ylist("SELECT * FROM `@#_zg_shangpin` where `id` in($shopids)",array("key"=>"id"));
		}

		
		if(!empty($shoparr)){
			foreach($shoparr as $key=>$val){
				if($val['q_end_time']=='' || $val['q_end_time']==NULL){
					$yyslist[$key]=$val;
					$Mcartlist[$val['id']]['num'] = $Mcartlist[$val['id']]['num'];
					$Mcartlist[$val['id']]['shenyu']=$val['shenyurenshu'];
					$Mcartlist[$val['id']]['money']=$val['yunjiage'];
					$Mcartlist[$val['id']]['expressFee'] = substr(sprintf("%.3f",($val['expressFee'])),0,-1);
					$Mcartlist[$val['id']]['subType'] = $val['subType'];
				}
			}
			_setcookie('Cartlistzg',json_encode($Mcartlist),'');
		}
	 

		$MoenyCount=0;
		$gouwucheshopinfo='{';
		if(count($yyslist)>=1){
			foreach($yyslist as $key => $val){
				//$key=intval($key);					
				if(isset($yyslist[$key]) && $key != 'MoenyCount'){
					$Yf = $yyslist[$key]['expressFee'];	
					$yyslist[$key]['cart_gorenci'] = $Mcartlist[$key]['num'];//$yyslist[$key]['zongrenshu'];//$val['num'] ? $val['num'] : 1;
					$MoenyCount += ($yyslist[$key]['yunjiage'] * $Mcartlist[$key]['num']) + $Yf;	

					$yyslist[$key]['cart_xiaoji'] = substr(sprintf("%.3f",($MoenyCount)),0,-1);			
					
					//引入一个单价
					$yyslist[$key]['thisMoney'] = $yyslist[$key]['yunjiage'] + $Yf;	
					
					$yyslist[$key]['thisMoneyC'] = $yyslist[$key]['yunjiage'] * $Mcartlist[$key]['num'] + $Yf;	
					
					$yyslist[$key]['cart_shenyu'] = 0;//$yyslist[$key]['zongrenshu'] - $yyslist[$key]['canyurenshu'];
					$yyslist[$key]['expressFee'] =  substr(sprintf("%.3f",($Yf)),0,-1);	//$yyslist[$key]['zongrenshu'] - $yyslist[$key]['canyurenshu'];
					$yyslist[$key]['num'] = $Mcartlist[$key]['num'];
					
					//新增属性部分
					$yyslist[$key]['subType'] = $Mcartlist[$key]['subType'];

					$yyslist[$key]['delId'] = $key;
					$subType = json_decode($yyslist[$key]['subType'],true);
		

					foreach($subType as $k => $v){
						$MoenyCount += $v['price'] * $Mcartlist[$key]['num'];
						$yyslist[$key]['cart_xiaoji'] += $v['price'] * $Mcartlist[$key]['num'];
						$yyslist[$key]['thisMoney'] += $v['price'];
						$yyslist[$key]['thisMoneyC'] += $v['price'] * $Mcartlist[$key]['num'];
					}

					$yyslist[$key]['cart_xiaoji'] = number_format($yyslist[$key]['cart_xiaoji'],2);
					$gouwucheshopinfo.="'$key':{'shenyu':'".$yyslist[$key]['cart_shenyu']."','num':'".$yyslist[$key]['num']."','money':'".$MoenyCount."','subType':'".$val['subType']."'},";
				}
			}
		}


		$shop=0;		
		if(!empty($yyslist)){
		   $shop=1;
		}
		//echo "<pre>";
		//print_r($Mcartlist);
		$MoenyCount=substr(sprintf("%.3f",$MoenyCount),0,-1);	
		$gouwucheshopinfo.="'MoenyCount':$MoenyCount}";		
		

		include templates("mobile/cartzg","cartlistzg");
	}
	
	
	//支付界面
	public function payzg(){  
        $webname=$this->_yys['web_name'];	
		$biaoti="支付界面";
		$index="i3";
		parent::__construct();	
		if(!$huiyuan=$this->userinfo){
		  header("location: ".LOCAL_PATH."/mobile/user/login");	
		}		
		$Mcartlist=json_decode(stripslashes($this->Cartlistzg),true);

		$shopids='';	
		if(is_array($Mcartlist)){			
			foreach($Mcartlist as $key => $val){
				if($key != 'MoenyCount'){
					$shopids .= $key.',';
				}
				//$shopids .= intval($key).',';					
			}
			$shopids=str_replace(',0','',$shopids);
			$shopids=trim($shopids,',');
	
		}

		$yyslist=array();
		if($shopids!=NULL){
			//改造YYSLIST
			$shopidsArray = explode(',',$shopids);
			foreach($shopidsArray as $key => $one){
				if(is_numeric($one)){
					$shopinfo = $this->db->YOne("select * from `@#_zg_shangpin` where `id` = '$one'");
				}elseif($one != 'MoenyCount'){
					$ids = explode('_',$one);
					$ids = $ids['0'];
					$shopinfo = $this->db->YOne("select * from `@#_zg_shangpin` where `id` = '$ids'");
				}
				$yyslist[$one] = $shopinfo;	
			}
			//$yyslist=$this->db->Ylist("SELECT * FROM `@#_zg_shangpin` where `id` in($shopids)",array("key"=>"id"));
		}

		$MoenyCount=0;
		if(count($yyslist)>=1){
			foreach($Mcartlist as $key => $val){
				//$key=intval($key);					
				if(isset($yyslist[$key]) && $key != 'MoenyCount'){
					$Yf = $yyslist[$key]['expressFee'];	
					$yyslist[$key]['cart_gorenci']= $Mcartlist[$key]['num'];//1;//$yyslist[$key]['zongrenshu'];//$val['num'] ? $val['num'] : 1;
					$MoenyCount += $yyslist[$key]['yunjiage'] * $Mcartlist[$key]['num'] + $Yf;
					$yyslist[$key]['cart_xiaoji'] = substr(sprintf("%.3f",($MoenyCount)),0,-1);			
					$yyslist[$key]['cart_shenyu'] = 0;//$yyslist[$key]['zongrenshu'] - $yyslist[$key]['canyurenshu'];
					
					//引入一个单价
					$yyslist[$key]['thisMoney'] = $yyslist[$key]['yunjiage'] + $Yf;	
					
					$yyslist[$key]['cart_shenyu'] = 0;//$yyslist[$key]['zongrenshu'] - $yyslist[$key]['canyurenshu'];
					$yyslist[$key]['expressFee'] =  substr(sprintf("%.3f",($Yf)),0,-1);	//$yyslist[$key]['zongrenshu'] - $yyslist[$key]['canyurenshu'];
					$yyslist[$key]['num'] = $Mcartlist[$key]['num'];
					
					//新增属性部分
					$yyslist[$key]['subType'] = $Mcartlist[$key]['subType'];

					$yyslist[$key]['delId'] = $key;
					$subType = json_decode($yyslist[$key]['subType'],true);
					foreach($subType as $k => $v){
						$MoenyCount += $v['price'] * $Mcartlist[$key]['num'];
						$yyslist[$key]['cart_xiaoji'] += $v['price'] * $Mcartlist[$key]['num'];
						$yyslist[$key]['thisMoney'] += $v['price'] * $Mcartlist[$key]['num'];
					}

					$yyslist[$key]['cart_xiaoji'] = number_format($yyslist[$key]['cart_xiaoji'],2);
					$gouwucheshopinfo.="'$key':{'shenyu':'".$yyslist[$key]['cart_shenyu']."','num':'".$yyslist[$key]['num']."','money':'".$MoenyCount."','subType':'".$val['subType']."'},";
				}
			}

			$shopnum=0;  //表示有商品
		}else{
			_setcookie('Cartlistzg',NULL);
			//_note("购物车没有商品!",LOCAL_PATH);
			$shopnum=1; //表示没有商品
		}	
		
		//总支付价格
		$MoenyCount=substr(sprintf("%.3f",$MoenyCount),0,-1);
		//会员余额
		$Money=$huiyuan['money'];
		//商品数量
		$shoplen=count($yyslist);

		$fufen = System::DOWN_App_config("user_fufen",'','member');
		if($fufen['fufen_yuan']){
			$fufen_dikou = intval($huiyuan['score'] / $fufen['fufen_yuan']);
		}else{
			$fufen_dikou = 0;
		}
		$paylist = $this->db->Ylist("select * from `@#_payment` where `pay_start` = '1'");
		 
		session_start();
		$_SESSION['submitcode'] = $submitcode = uniqid();
		include templates("mobile/cartzg","paymentzg");
	}
	
	//开始支付
	public function paysubmitzg(){
		$webname=$this->_yys['web_name'];
		$biaoti="支付结算";		
		$index="i3";
		header("Cache-control: private");
		parent::__construct();	
		if(!$this->userinfo){
		  header("location: ".LOCAL_PATH."/mobile/user/login");
		  exit;
		}
		
		session_start();	


		 $checkpay=$this->segment(4);//获取支付方式  fufen   money  bank
		 $banktype=$this->segment(5);//获取选择的银行 CMBCHINA  ICBC CCB
		 $money=$this->segment(6);   //获取需支付金额
		 

		 $fufen=$this->segment(7);   //获取福分
		 //echo "checkpay".$checkpay."banktype".$banktype."money".$money."fufen".$fufen;

		 $uid = $this->userinfo['uid'];
		 if($banktype=="chinabankwap"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'chinabankwap' and `pay_start`='1' ");
		 }else if($banktype=="ICBC-WAP"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'wxpay' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="alipay1"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'alipay1' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="tenpay1"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'tenpay1' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="baowap"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'baowap' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="yunpay"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'yunpay' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else
		 {
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'malipay' and `pay_start`='1' ");
		 }
		/*
		 if(!$zhifutype){		 
			_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 } 
		 */
		// var_dump($pay_type_id);
		 //exit;
		 $pay_checkbox=false;
		 $pay_type_bank=false;
		 $pay_type_id=false;
		 
		 if($checkpay == 'money' || $checkpay == 'fufen'){ 
			$pay_checkbox=true;
		 }
	      
		 if($banktype != 'nobank'){		  
		   $pay_type_id=$banktype;
		 }
		 
		 if(!empty($zhifutype)){
		    $pay_type_bank = $zhifutype['pay_class'];
		 }
		 

		if(!$pay_type_id){
          if($checkpay != 'fufen' && $checkpay != 'money')		  
			_notemobile("选择支付方式");		  
		}
		
	

		/*************
			start
		*************/		
		

		$pay=System::DOWN_App_class('pay','pay');
		$pay->fufen = 0;
		if($checkpay == 'fufen'){
			$pay->fufen = $fufen;
		}
		
		$pay->pay_type_bank = $pay_type_bank;

		$ok = $pay->init($uid,$pay_type_id,'go_record',null,'zg');	//抢购商品
		if($ok != 'ok'){
			_setcookie('Cartlistzg',NULL);		
			_notemobile("购物车没有商品请<a href='".LOCAL_PATH."/mobile/cartzg/cartlistzg' style='color:#22AAFF'>返回购物车</a>查看");
		}		
		//print_r($pay);
		//exit;
		$check = $pay->start_pay($pay_checkbox,'zg');		
		if(!$check){
			_notemobile("订单添加失败,请<a href='".LOCAL_PATH."/mobile/cartzg/cartlistzg' style='color:#22AAFF'>返回购物车</a>查看");
		}
		if($check){
			//成功
			$orderId = $pay->orderId;
			header("location: ".LOCAL_PATH."/mobile/cartzg/paysuccesszg/".$orderId);
		}else{
			//失败	
			_setcookie('Cartlistzg',NULL);
			header("location: ".LOCAL_PATH."/mobile/mobile");
		}		
		exit;
	}
	
	//成功页面
	public function paysuccesszg(){
		$biaoti="支付成功";
		$index="i3";
		$uid=intval(_encrypt(_getcookie("uid"),'DECODE'));
		$this->userinfo=$this->db->YOne("SELECT * from `@#_yonghu` where `uid` = '$uid'");
		$Mcartlist=json_decode(stripslashes($this->Cartlistzg),true);
		$shopids='';	
		if(is_array($Mcartlist)){			
			foreach($Mcartlist as $key => $val){
				$shopids.=intval($key).',';				
			}
			$shopids=str_replace(',0','',$shopids);
			$shopids=trim($shopids,',');
	
		}	 
		$yyslist=array();
		if($shopids!=NULL){
			$yyslist=$this->db->Ylist("SELECT * FROM `@#_zg_shangpin` where `id` in($shopids)",array("key"=>"id"));
		}
		foreach($yyslist as $k=>$v){
			push_weixin_msg(1,$this->userinfo['openid'],$this->userinfo['username'],$this->userinfo['uid'],$v['title'],$v['qishu']);
		}
	    $webname=$this->_yys['web_name'];
		_setcookie('Cartlistzg',NULL);
		$dingdancode = $this->segment(4);
		include templates("mobile/cartzg","paysuccesszg");		
	}
	
	//充值
	public function addmoneyzg(){
		parent::__construct();	
		$webname=$this->_yys['web_name'];
		 $money=$this->segment(4);//获取充值金额
		 $banktype=$this->segment(5); 		 
		if(!$this->userinfo){
		  header("location: ".LOCAL_PATH."/mobile/user/login");
		  exit;
		}	
         
         if($banktype=="chinabankwap"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'chinabankwap' and `pay_start`='1' ");
		 }else if($banktype=="wxpay"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'wxpay' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="alipay1"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'alipay1' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="tenpay1"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'tenpay1' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="yunpay"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'yunpay' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else if($banktype=="baowap"){
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'baowap' and `pay_start`='1' ");
			//_notemobile("手机支付只支持网银在线支付,请重新支付，谢谢！");
		 }else
		 {
			$zhifutype = $this->db->YOne("select * from `@#_payment` where `pay_class` = 'malipay' and `pay_start`='1' ");
		 }
		 if(!$zhifutype){
			_notemobile("手机支付只支持网银在线,请联系站长开通！");
		 }	  
		 
		 if(!empty($zhifutype)){
		    $pay_type_bank=$zhifutype['pay_class'];
		 }
		 
		 $pay_type_id=$banktype;
		 
		 
		 
		//$pay_type_bank=isset($_POST['pay_bank']) ? $_POST['pay_bank'] : false;
		//$pay_type_id=isset($_POST['account']) ? $_POST['account'] : false;		
		//$money=intval($_POST['money']);
		$uid = $this->userinfo['uid'];
		$pay=System::DOWN_App_class('pay','pay');
		$pay->pay_type_bank = $pay_type_bank;
		$pay->init($uid,$pay_type_id,'addmoney_record',$money);
	}
}


?>